package com.fafa.pos.dao.impl;

import android.content.ContentValues;
import android.database.Cursor;
import com.fafa.pos.dao.DBUtil;
import com.fafa.pos.dao.IVipGroupInfoDao;
import com.fafa.pos.pojo.CardInfo;
import com.fafa.pos.pojo.VipGroupInfo;
import com.fafa.pos.pojo.VipInfo;
import com.fafa.pos.util.DateUtil;

import java.util.List;

/**
 * Created by ZhouBing on 2017/8/10.
 *
 * SQLite  --  T_VIPGROUPINFO
 */
public class VipGroupInfoDaoImpl implements IVipGroupInfoDao{

    private DBUtil dbUtil;

    private ConfigDaoImpl configDao;

    public VipGroupInfoDaoImpl(DBUtil dbUtil) {
        this.dbUtil = dbUtil;
        configDao = new ConfigDaoImpl(dbUtil);
    }

    @Override
    public void saveVipGroupInfoList(List<VipGroupInfo> vipGroupInfos) {

        dbUtil.getDB().beginTransaction();

        try{
            for (VipGroupInfo vipGroupInfo:vipGroupInfos) {

                //Id存在则更新不存在新增
                String sql = "select GROUP_ID from T_VIPGROUPINFO where GROUP_ID = ?";
                Cursor cursor = dbUtil.getDB().rawQuery(sql,new String[]{vipGroupInfo.getGroupId()});

                ContentValues contentValues = new ContentValues();
                contentValues.put("GROUP_ID",vipGroupInfo.getGroupId());
                contentValues.put("GROUP_CODE",vipGroupInfo.getGroupCode());
                contentValues.put("GROUP_NAME",vipGroupInfo.getGroupName());
                contentValues.put("LEVEL_ID",vipGroupInfo.getLevelId());
                contentValues.put("RATE",vipGroupInfo.getRate());
                contentValues.put("STATUS",vipGroupInfo.getStatus());
                contentValues.put("VISIBLE",vipGroupInfo.getVisible());
                contentValues.put("UPDATE_DATE",vipGroupInfo.getUpdate());
                if(cursor.moveToNext()){
                    dbUtil.getDB().update("T_VIPGROUPINFO",contentValues," GROUP_ID = ?",new String[]{vipGroupInfo.getGroupId()});
                }else{
                    dbUtil.getDB().insert("T_VIPGROUPINFO",null,contentValues);
                }
                if(cursor!=null){
                    cursor.close();
                }
            }
            configDao.saveItem("T_VIPGROUPINFO_UPDATE_DATE", DateUtil.getCurDateStr());
            dbUtil.getDB().setTransactionSuccessful();
        }finally {
            dbUtil.getDB().endTransaction();
            dbUtil.close();
        }
    }

    @Override
    public VipGroupInfo getVipGroupInfo(String groupId) {

        String sql = "select * from T_VIPGROUPINFO where GROUP_ID = ?";
        Cursor cursor = dbUtil.getDB().rawQuery(sql,new String[]{groupId});
        if(cursor.moveToNext()){
            VipGroupInfo vipGroupInfo = new VipGroupInfo();

            vipGroupInfo.setGroupId(cursor.getString(0));
            vipGroupInfo.setGroupCode(cursor.getString(1));
            vipGroupInfo.setGroupName(cursor.getString(2));
            vipGroupInfo.setLevelId(cursor.getString(3));
            vipGroupInfo.setRate(cursor.getString(4));
            vipGroupInfo.setStatus(cursor.getString(5));
            vipGroupInfo.setVisible(cursor.getString(6));
            vipGroupInfo.setUpdate(cursor.getString(7));

            return  vipGroupInfo;
        }
        if(cursor!=null){
            cursor.close();
        }
        dbUtil.close();
        return null;
    }
}
